package com.ljh.onlinetest.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ljh.onlinetest.base.BaseApiController;
import com.ljh.onlinetest.base.RestResponse;
import com.ljh.onlinetest.domain.ExamPaperAnswer;
import com.ljh.onlinetest.domain.Subject;
import com.ljh.onlinetest.domain.User;
import com.ljh.onlinetest.service.ExamPaperAnswerService;
import com.ljh.onlinetest.service.SubjectService;
import com.ljh.onlinetest.service.UserService;
import com.ljh.onlinetest.utils.DateTimeUtil;
import com.ljh.onlinetest.utils.ExamUtil;
import com.ljh.onlinetest.utils.PageInfoHelper;
import com.ljh.onlinetest.viewmodel.admin.paper.ExamPaperAnswerPageRequestVM;
import com.ljh.onlinetest.viewmodel.admin.paper.ExamPaperAnswerPageResponseVM;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author ljh
 * @ClassName ExamPaperAnswerController.java
 * @createTime 2022年02月22日
 */
@RestController("AdminExamPaperAnswerController")
@RequestMapping("/api/admin/examPaperAnswer")
public class ExamPaperAnswerController extends BaseApiController {
    private final ExamPaperAnswerService examPaperAnswerService;
    private final SubjectService subjectService;
    private final UserService userService;

    @Autowired
    public ExamPaperAnswerController(ExamPaperAnswerService examPaperAnswerService, SubjectService subjectService, UserService userService) {
        this.examPaperAnswerService = examPaperAnswerService;
        this.subjectService = subjectService;
        this.userService = userService;
    }

    @PostMapping("/page")
    public RestResponse<PageInfo<ExamPaperAnswerPageResponseVM>> pageJudgeList(@RequestBody ExamPaperAnswerPageRequestVM model){
        PageInfo<ExamPaperAnswer> paperAnswers = examPaperAnswerService.page(model);
        PageInfo<ExamPaperAnswerPageResponseVM> pageInfo = PageInfoHelper.copyMap(paperAnswers, e -> {
            ExamPaperAnswerPageResponseVM responseVM = modelMapper.map(e, ExamPaperAnswerPageResponseVM.class);
            Subject subject = subjectService.selectById(responseVM.getSubjectId());
            responseVM.setDoTime(ExamUtil.secondToVM(e.getDoTime()));
            responseVM.setSystemScore(ExamUtil.scoreToVM(e.getSystemScore()));
            responseVM.setUserScore(ExamUtil.scoreToVM(e.getUserScore()));
            responseVM.setPaperScore(ExamUtil.scoreToVM(e.getPaperScore()));
            responseVM.setSubjectName(subject.getName());
            responseVM.setCreateTime(DateTimeUtil.dateFormat(e.getCreateTime()));
            User user = userService.selectById(e.getCreateUser());
            responseVM.setUserName(user.getUserName());
            return responseVM;
        });
        return RestResponse.ok(pageInfo);
    }
}
